/*
 * Copyright 2010, 2011 Matt Oliveri
 *
 * This file is part of JConfigGen
 *
 * JConfigGen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JConfigGen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JConfigGen.  If not, see <http://www.gnu.org/licenses/>.
 */

package atma.jconfiggen.sax;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import atma.jconfiggen.Expr;
import atma.jconfiggen.Proxy;
import atma.jconfiggen.NewObj;
import atma.jconfiggen.PxRef;
import atma.jconfiggen.Call;

abstract class HFact extends HandlerHelper
{
	static NewH mkNew
	(
		SAXHandler saxh,ArrayList<Expr> pnt,
		String c,String t,String id,String a,
		int extc,int[] extv,Attributes attr
	)
	{
		if (a == null)
		{
			// Pack the stuff we need from attr into a String[], because attr might change to reflect some other event by the time we would use it.
			String[] atts = new String[extc * 2];
			for (int i = 0,j = 0;i < extc;++i,j += 2)
			{
				int k = extv[i];
				atts[j] = attr.getLocalName(k);
				atts[j + 1] = attr.getValue(k);
			}
			return new LateNewH(saxh,pnt,c,t,id,atts);
		}
		return new NewH(saxh,pnt).init(new NewObj(saxh.config,c,t,id,a),extc,extv,attr);
	}

	static ElemH mkCall(SAXHandler saxh,ArrayList<Expr> pnt,String t,String i,String m,String a) throws SAXException
	{
		int ln = saxh.loc.getLineNumber();
		Proxy w = saxh.getWith();
		if (a == null) return new CallH(saxh,pnt,w,ln,t,i,m);
		pnt.add(new Call(saxh.config,ln,t,i,new PxRef(w),m,a));
		return new NoElemH(saxh,"Cannot have child elements in a call that uses the args attribute");
	}

	HFact(SAXHandler s) {super(s);}
	abstract ElemH make(ArrayList<Expr> pnt,Attributes attr) throws SAXException;
}
